package legumes.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Serialized;

/**
 * The order relative to the proposed Menu.
 * 
 */
public class OrderPartMenuItem implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id private long id;
	private Menu menu;
	private String menuCount;
	@Serialized private List<MenuItem> removedMenuItems;

	public OrderPartMenuItem(){
		menuCount = MenuCount.ZERO_MENU.toString();
		removedMenuItems = new ArrayList<MenuItem>();
	}
	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	public boolean isRemoved(MenuItem mi_) {
		return getRemovedMenuItems().contains(mi_);
	}

	/**
	 * @param menu
	 *            the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	
	public float getPrice() {
		switch (getMenuCountAsEnum()) {
			case ONE_MENU:
				return getMenu().getWholePrice();
			case HALF_MENU:
				return getMenu().getHalfPrice();
			case ZERO_MENU:
				return 0f;
		}
		throw new RuntimeException("missing case for enum MenuCountEnum");
	}

//	/**
//	 * Takes into account removed menu items.
//	 * 
//	 * @return
//	 */
//	public Cost computeRealPrice() {
//		float price = getPrice();
//		Cost cost = new Cost(price);
//
//		for (MenuItem removedMenuItem : getRemovedMenuItems()) {
//
//			// copy menu items to easily compute price according
//			// to menu count
//			MenuItem mi = new MenuItem(removedMenuItem);
//			switch (getMenuCountAsEnum()) {
//			case ONE_MENU:
//				break;
//			case HALF_MENU:
//				mi.getQuantity().div(2);
//				break;
//			case ZERO_MENU:
//				mi.getQuantity().setValue(0);
//				break;
//			}
//
//			cost.minus(mi.computeRealPrice());
//		}
//		return cost;
//	}

	/**
	 * @return the menuCount
	 */
	public String getMenuCount() {
		return menuCount;
	}

	public MenuCount getMenuCountAsEnum() {
		return MenuCount.valueOf(menuCount);
	}

	public String getMenuCountAsDisplayName() {
		return getMenuCountAsEnum().getDisplayName();
	}

	/**
	 * @param menuCount
	 *            the menuCount to set
	 */
	public void setMenuCount(String menuCount) {
		this.menuCount = menuCount;
	}

	/**
	 * @return the removedMenuItems
	 */
	public List<MenuItem> getRemovedMenuItems() {
		return removedMenuItems;
	}

	/**
	 * @param removedMenuItems
	 *            the removedMenuItems to set
	 */
	public void setRemovedMenuItems(List<MenuItem> removedMenuItems) {
		this.removedMenuItems = removedMenuItems;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[OrderPartMenuItem ").append(" menuCount=").append(
				getMenuCount()).append(" removedMenuItems=").append(
				getRemovedMenuItems()).append("]");

		return sb.toString();
	}

	public void addRemovedMenuItems(MenuItem menuItem_) {
		removedMenuItems.add(menuItem_);
	}

	public void setMenuCount(MenuCount menuCount_) {
		setMenuCount(menuCount_.toString());
	}

	public void removeRemovedItem(MenuItem menuItem) {
		getRemovedMenuItems().remove(menuItem);
	}

	public void removeRemovedItems() {
		getRemovedMenuItems().clear();
	}

}
